package juc.juccore.atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * 累加器用法
 */
public class LongAccumulatorDemo {
    public static void main(String[] args) {
        LongAccumulator longAccumulator =
                new LongAccumulator((left, right) -> left + right, 0);
//        longAccumulator.accumulate(1);
//        longAccumulator.accumulate(2);
//        System.out.println(longAccumulator.get());
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        IntStream.range(1, 10).forEach(value -> executorService.submit(() -> {
            longAccumulator.accumulate(value);
        }));
        executorService.shutdown();
        while (!executorService.isTerminated()) {
        }
        System.out.println(longAccumulator.get());
    }

}
